home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
Acere (Card Game)
/
AcereÄ.sit
/
Acereƒ
/
WellStack.cp
< prev
next >
Wrap
Text File
|
1994-08-25
|
9KB
|
398 lines
// ===========================================================================
// WellStack.cp ⌐1993 Metrowerks Inc. All rights reserved.
// ===========================================================================
//
// Class for an object that can draw itself and respond to mouse clicks
#ifdef PowerPlant_PCH
#include PowerPlant_PCH
#endif
#include "WellStack.h"
#include "CardDeck.h"
#include "AcereApp.h"
#include "CTextDoc.h"
#include <LView.h>
#include <LStream.h>
#include <PP_Messages.h>
#include <UDrawingState.h>
extern CTextDoc *theDoc;
// ---------------------------------------------------------------------------
// Ñ CreatePaneStream [static]
// ---------------------------------------------------------------------------
// Return a new Pane object initialized using data from a Stream
WellStack*
WellStack::CreateWellStack(
LStream *inStream)
{
return (new WellStack(inStream));
}
// ---------------------------------------------------------------------------
// Ñ WellStack()
// ---------------------------------------------------------------------------
// Default Constructor
WellStack::WellStack()
{
mPaneID = 0;
mFrameSize.width = mFrameSize.height = 0;
mFrameLocation.h = mFrameLocation.v = 0;
mUserCon = 0;
mFrameBinding.left =
mFrameBinding.top =
mFrameBinding.right =
mFrameBinding.bottom = false;
mVisible = mActive = mEnabled = triState_Latent;
mSuperView = nil;
itsCard.card = kNoCard;
itsCards = nil;
InitCards();
}
// ---------------------------------------------------------------------------
// Ñ WellStack(const WellStack&)
// ---------------------------------------------------------------------------
// Copy Constructor
WellStack::WellStack(
const WellStack &inOriginal)
{
// Copy members of Original
mPaneID = inOriginal.mPaneID;
mFrameSize = inOriginal.mFrameSize;
mFrameLocation = inOriginal.mFrameLocation;
mFrameBinding = inOriginal.mFrameBinding;
mUserCon = inOriginal.mUserCon;
mSuperView = nil; // Copy is not inside any View
// Pane properties. If Original has
// property ON, Copy is Latent.
mVisible = inOriginal.mVisible;
if (mVisible == triState_On) {
mVisible = triState_Latent;
}
mActive = inOriginal.mActive;
if (mActive == triState_On) {
mActive = triState_Latent;
}
mEnabled = inOriginal.mEnabled;
if (mEnabled == triState_On) {
mEnabled = triState_Latent;
}
itsCards = nil;
InitCards();
}
// ---------------------------------------------------------------------------
// Ñ WellStack(SPaneInfo&)
// ---------------------------------------------------------------------------
// Construct Pane from data in a struct
WellStack::WellStack(
const SPaneInfo &inPaneInfo)
{
InitPane(inPaneInfo);
}
// ---------------------------------------------------------------------------
// Ñ WellStack(LStream*)
// ---------------------------------------------------------------------------
// Construct Pane from data in a Stream
WellStack::WellStack(
LStream *inStream)
{
SPaneInfo thePaneInfo;
inStream->ReadData(&thePaneInfo, sizeof(SPaneInfo));
InitPane(thePaneInfo);
}
// ---------------------------------------------------------------------------
// Ñ InitPane
// ---------------------------------------------------------------------------
// Initialize Pane from data in a struct
void
WellStack::InitPane(
const SPaneInfo &inPaneInfo)
{
mPaneID = inPaneInfo.paneID;
mFrameSize.width = inPaneInfo.width;
mFrameSize.height = inPaneInfo.height;
mUserCon = inPaneInfo.userCon;
mVisible = triState_Off;
if (inPaneInfo.visible) {
mVisible = triState_Latent;
}
mActive = triState_Latent;
mEnabled = triState_Off;
if (inPaneInfo.enabled) {
mEnabled = triState_Latent;
}
mFrameBinding = inPaneInfo.bindings;
mSuperView = nil;
LView *theSuperView = inPaneInfo.superView;
if (theSuperView == Default_SuperView) {
theSuperView = GetDefaultView();
}
PutInside(theSuperView);
if (theSuperView != nil) {
PlaceInSuperImageAt(inPaneInfo.left, inPaneInfo.top, false);
Boolean expandHoriz = (inPaneInfo.width < 0);
Boolean expandVert = (inPaneInfo.height < 0);
if (expandHoriz || expandVert) {
theSuperView->ExpandSubPane(this, expandHoriz, expandVert);
}
}
itsCards = nil;
InitCards();
}
// ---------------------------------------------------------------------------
// Ñ ~WellStack
// ---------------------------------------------------------------------------
// Destructor
WellStack::~WellStack()
{
PutInside(nil);
if (sLastPaneClicked == this) {
sLastPaneClicked = nil;
}
}
// ---------------------------------------------------------------------------
// Ñ Draw
// ---------------------------------------------------------------------------
// Try to draw contents of a Pane
//
// inSuperDrawRgnH specifies, in Port coordinates, the portion of the
// Pane's SuperView that needs to be drawn. Specify nil to bypass
// the intersection test.
//
// This is a wrapper function which calls DrawSelf if it is proper for
// the Pane to draw. This means that:
// > Pane's Visible property is on
// > Pane can be focused
// > Pane's Frame is in QuickDraw space
// > Pane's Frame intersects inSuperDrawRgnH
void WellStack::Draw(RgnHandle inSuperDrawRgnH)
{
Rect frame, useFrame;
short i, maxItems;
CardStruct currentCard;
maxItems = (short) itsCards->GetCount();
if ( IsVisible() &&
FocusDraw() &&
CalcPortFrameRect(frame) &&
((inSuperDrawRgnH == nil) || RectInRgn(&frame, inSuperDrawRgnH)) )
{
if (maxItems == 0)
{
useFrame = frame;
useFrame.bottom = useFrame.top + 90;
EraseRect(&useFrame);
FrameRoundRect(&useFrame, 20, 20);
}
for (i=1; i<= maxItems; i++)
{
itsCards->FetchItemAt(i, ¤tCard);
if (i == maxItems)
{
useFrame = frame;
useFrame.top = frame.bottom - 90;
}
else
{
useFrame = frame;
useFrame.top = frame.top + (20 * (i-1));
useFrame.bottom = useFrame.top + 20;
}
if (currentCard.card == kNoCard)
{
EraseRect(&frame);
FrameRoundRect(&useFrame, 20, 20);
}
else
{
theDeck->DrawCard(¤tCard, useFrame);
// FrameRoundRect(&useFrame, 20, 20);
}
}
}
}
void WellStack::InitCards(void)
{
// done when initializing only
short i, k;
long thePaneID = GetPaneID();
theDoc->theStacks[theDoc->currentStack] = this;
if (itsCards != nil) // we're restarting a game╔
delete itsCards;
itsCards = new LDynamicArray(sizeof(CardStruct));
mFrameSize.height -= 20;
for (i=(thePaneID - 3001); i < NumCards; i += 8)
{
k = (short) theDeck->theCardPositions[i];
theDeck->GetCardInfo(k, &itsCard);
itsCard.itsOwner = this;
// itsCards->InsertItemsAt(1, 9999, &itsCard);
AddCardToWell(nil, &itsCard);
// whichCard = k;
}
theDoc->currentStack++;
Draw(0L);
}
Boolean WellStack::CanDropOnSlot(CardStruct *draggedCard)
{
// stack behavior is different: we need a card of an opposite color
// and an immediately lower value.
if ((draggedCard->color != itsCard.color) && (draggedCard->card == (itsCard.card -1)))
return (true);
else
return (false);
}
void WellStack::AddCardToWell(CardWell *whichWell, CardStruct *whichCard)
{
whichCard->itsOwner = this;
itsCards->InsertItemsAt(1, 9999, whichCard);
itsCard = *whichCard;
AdjustFrame();
}
void WellStack::RemoveCardFromWell(CardWell *whichWell, CardStruct *whichCard)
{
short i, maxItems;
CardStruct tempCard;
maxItems = (short) itsCards->GetCount();
for (i = maxItems; i > 0; i--)
{
itsCards->FetchItemAt(i, &tempCard);
if ((tempCard.suit == whichCard->suit) &&
(tempCard.card == whichCard->card)) // right card
{
itsCards->RemoveItemsAt(1, i);
if (i == maxItems) // need to update "top" card
{
maxItems = (short) itsCards->GetCount();
if (maxItems == 0)
itsCard.card = kNoCard;
else
itsCards->FetchItemAt(maxItems, &itsCard);
}
AdjustFrame();
break; // exit for loop
}
}
}
void WellStack::AdjustFrame(void)
{
short maxItems, difference;
Rect frame;
RgnHandle newRgn, oldRgn, saveClipRgn;
oldRgn = NewRgn();
OpenRgn();
CalcPortFrameRect(frame);
FrameRoundRect(&frame, 20, 20);
CloseRgn(oldRgn);
newRgn = NewRgn();
difference = mFrameSize.height;
maxItems = (short) itsCards->GetCount();
mFrameSize.height = 70 + (20 * maxItems);
difference -= mFrameSize.height;
if (difference > 0)
{
// we need to clean up
CalcPortFrameRect(frame);
OpenRgn();
FrameRoundRect(&frame, 20, 20);
CloseRgn(newRgn);
// DiffRgn(newRgn,oldRgn,newRgn);
DiffRgn(oldRgn, newRgn, newRgn);
saveClipRgn = NewRgn();
GetClip(saveClipRgn);
SetClip(newRgn);
EraseRgn(newRgn);
SetClip(saveClipRgn);
DisposeRgn(saveClipRgn);
}
DisposeRgn(newRgn);
DisposeRgn(oldRgn);
}